package com.example.controller;


import com.example.common.R;
import com.example.entity.User;
import com.example.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    private UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/login")
    public R login(@RequestBody User user) throws JsonProcessingException {
        String jwt = userService.login(user);
        if (StringUtils.hasLength(jwt)) {
            return R.ok().Message("登录成功").Data("token", jwt);
        }
        return R.error().Message("登录失败");
    }
}

